package com.yl.order.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.net.yl.entity.ActivateEntity;
import com.yl.common.utils.CacheFileUtil;
import com.yl.order.BuildConfig;
import com.yl.order.Constants;
import com.yl.order.entity.SyncDeviceWrap;

/**
 * create by cjx on 2024/4/21
 * desc:
 */
public class ConfigModel {

    private static final String DEVICE_FILE_NAME = ".serial_port";
    private static final String DEVICE_FILE_TEMPERATURE_NAME = ".temperature_serial_port";
    private static final String DEVICE_FILE_WEIGHT_NAME = ".weight_serial_port";
    private static final String ACTIVATE_FILE_NAME = ".activate";
    private static final String CONFIG_CLEAN_FLOW_FILE_NAME = ".config_clean_flow";

    private static final String SYNC_MOCK_NAME = ".sync_mock";



    public static SyncDeviceWrap getSyncDeviceWrap() {
        String cache = CacheFileUtil.getInstance().readCacheInfoAes(Constants.FILE_IMPORT_DIR, ACTIVATE_FILE_NAME, BuildConfig.AES_PASSWORD);
        if (cache == null) {
            return null;
        }
        return new Gson().fromJson(cache, new TypeToken<SyncDeviceWrap>() {
        }.getType());
    }

    public static void saveDeviceWrap(SyncDeviceWrap wrap) {
        if (wrap == null) {
            CacheFileUtil.getInstance().saveCacheInfoAes(Constants.FILE_IMPORT_DIR, "", ACTIVATE_FILE_NAME, BuildConfig.AES_PASSWORD);
            return;
        }
        Gson gson = new Gson();
        CacheFileUtil.getInstance().saveCacheInfoAes(Constants.FILE_IMPORT_DIR, gson.toJson(wrap), ACTIVATE_FILE_NAME, BuildConfig.AES_PASSWORD);
    }

    public static void saveActivate(ActivateEntity device) {
        if (device == null) {
            CacheFileUtil.getInstance().saveCacheInfoAes(Constants.FILE_IMPORT_DIR, "", ACTIVATE_FILE_NAME, BuildConfig.AES_PASSWORD);
            return;
        }
        Gson gson = new Gson();
        CacheFileUtil.getInstance().saveCacheInfoAes(Constants.FILE_IMPORT_DIR, gson.toJson(device), ACTIVATE_FILE_NAME, BuildConfig.AES_PASSWORD);
    }

    public static ActivateEntity getActivate() {
        String cache = CacheFileUtil.getInstance().readCacheInfoAes(Constants.FILE_IMPORT_DIR, ACTIVATE_FILE_NAME, BuildConfig.AES_PASSWORD);
        if (cache == null) {
            return null;
        }
        return new Gson().fromJson(cache, new TypeToken<ActivateEntity>() {
        }.getType());
    }

}
